import { PlusOutlined, FileExcelOutlined } from '@ant-design/icons';
import {
  ModalForm,
  ProForm,
  ProFormDateRangePicker,
  ProFormSelect,
  ProFormText,
  ProFormCheckbox,
} from '@ant-design/pro-components';
import { Button, message, notification } from 'antd';
import DownloadExcel from '@/utils/DownloadExcel.js';
import { useEffect, useState } from 'react';
import { useRequest } from 'umi';
import moment from 'moment';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const ExportExcel = ({
  tableName,
  auth,
  queryType,
  patentType,
  select,
}: any) => {
  const [columnsPrint, setColumnsPrint] = useState([]);
  const [checkedValueArray, setCheckedValueArray] = useState([]);
  const [dataReal, setDataReal] = useState([]);
  const [Key, setKey] = useState([]);

  const getColumns = useRequest(
    (data = { tableName: tableName }) => {
      return {
        url: 'http://10.1.40.84:9993/tilte',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data,
      };
    },
    {
      manual: true,
    },
  );
  const getColumnsData = useRequest(
    (data = { tableName: tableName }) => {
      return {
        url: 'http://10.1.40.84:9993/field',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data,
      };
    },
    {
      manual: true,
      onSuccess: () => {
      },
    },
  );
  const getColumnsData2 = useRequest(
    (data = { tableName: tableName }) => {
      return {
        url: 'http://10.1.40.84:9998/new/AllFieldController',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data,
      };
    },
    {
      manual: true,
      onSuccess: () => {
        console.log('撒旦发射点');
      },
    },
  );

  useEffect(() => {
    let columnsPrintTemp: any = [];
    getColumns.run().then((data) => {
      console.log(data);
      data.map((item: any) => {
        columnsPrintTemp.push(item.title);
      });
    });
    setColumnsPrint(columnsPrintTemp);
  }, []);

  return (
    <ModalForm
      title="导出Excel"
      trigger={
        <Button type="primary">
          <FileExcelOutlined />
          导出Excel
        </Button>
      }
      autoFocusFirstInput
      modalProps={{
        onCancel: () => console.log('run'),
        destroyOnClose: true,
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        switch (tableName) {
          case 'ACHIEVE_TRANS':
          case 'EQUIPMENT_DEVELOP':
          case 'SOFTWARE_COPYRIGHT':
          case 'STANDARD':
          case 'PAPER':
            getColumnsData
              .run({
                tableName: tableName,
                query: {
                  ...select,
                  auth: auth,
                  fields: Key,
                },
              })
              .then((e) => {
                DownloadExcel(
                  checkedValueArray,
                  e, //dataReal,
                  'EXCEL' + `${moment().format('YYYYMMDDHHmmss')}`,
                  [],
                  () => {
                    setTimeout(() => {
                      notification.success({ message: '导出成功!' });
                      console.log(checkedValueArray);
                    }, 300);
                  },
                );
              });
            break;
          case 'GRANT_PATENT':
            getColumnsData
              .run({
                tableName: tableName,
                query: {
                  ...select,
                  auth: auth,
                  fields: Key,
                  patentType: patentType,
                },
              })
              .then((e) => {
                DownloadExcel(
                  checkedValueArray,
                  e, //dataReal,
                  'EXCEL' + `${moment().format('YYYYMMDDHHmmss')}`,
                  [],
                  () => {
                    setTimeout(() => {
                      notification.success({ message: '导出成功!' });
                      console.log(checkedValueArray);
                    }, 300);
                  },
                );
              });
            break;
          case 'YEAR_AND_OTHER_ACHIEVE':
            getColumnsData
              .run({
                tableName: tableName,
                query: {
                  ...select,
                  auth: auth,
                  fields: Key,
                  yearOrOther: patentType,
                },
              })
              .then((e) => {
                DownloadExcel(
                  checkedValueArray,
                  e, //dataReal,
                  'EXCEL' + `${moment().format('YYYYMMDDHHmmss')}`,
                  [],
                  () => {
                    setTimeout(() => {
                      notification.success({ message: '导出成功!' });
                      console.log(checkedValueArray);
                    }, 300);
                  },
                );
              });
            break;
          default:
            getColumnsData2
              .run({
                tableName: tableName,
                query: {
                  ...select,
                  queryType: queryType,
                  fields: Key,
                },
              })
              .then((e) => {
                DownloadExcel(
                  checkedValueArray,
                  e,
                  'EXCEL' + `${moment().format('YYYYMMDDHHmmss')}`,
                  [],
                  () => {
                    setTimeout(() => {
                      notification.success({ message: '导出成功!' });
                      console.log(checkedValueArray);
                    }, 300);
                  },
                );
              });
            break;
        }
        return true;
      }}
    >
      <ProForm.Group>
        <ProFormCheckbox.Group
          name="checkbox-group"
          label="请选择导出列"
          // layout={"vertical"}
          options={columnsPrint}
          fieldProps={{
            onChange: (checkedValue) => {
              let checkedValueArrayTemp: any = [];
              let key: any = [];
              checkedValue.map((item: any) => {
                getColumns.data.map((itemsub: any) => {
                  if (item === itemsub.title) {
                    checkedValueArrayTemp.push(itemsub);
                    key.push(itemsub.dataIndex);
                  }
                });
              });
              setKey(key);
              setCheckedValueArray(checkedValueArrayTemp);
            },
            defaultValue:columnsPrint
          }}
        />
      </ProForm.Group>
    </ModalForm>
  );
};

export default ExportExcel;
